package cs236369.webserver;

import static org.junit.Assert.*;

import org.junit.Test;


public class ThreadPoolTest {

	int testing = 0;
	
	synchronized void add() {
		testing++;
	}
	
	class ThreadPoolSize implements Runnable {

		@Override
		public void run() {
			
			// wait a couple of seconds to allow other threads to claim jobs
			try {
				Thread.sleep(1000);
				add();
			} catch (InterruptedException ignore) {	}
		}
		
	}
	
	/**
	 * Checking that we do create x number of threads
	 * @throws Exception 
	 */
	@Test
	public void testThreadPoolSize() throws Exception {
		
		ThreadPool pool = new ThreadPool(3, 3);
		pool.start();
		
		// adding 3 runnables - so all threads get a job to do
		pool.addTask(new ThreadPoolSize());
		pool.addTask(new ThreadPoolSize());
		pool.addTask(new ThreadPoolSize());
		
		// allowing the runnables to finish
		Thread.sleep(2000);
		assertEquals(3,testing);
	}
	
	//@Test
	public void testShutDown() {
		// TODO test the shutdown
	}
}
